from typing import List

from fastapi import APIRouter, Depends, Query, Request
from sqlalchemy.orm import Session

from app.blog_service.schemas.blog import BlogResponse, BlogQuery, BlogCategoryResponse, BlogStatsResponse, \
    BlogLatestResponse, BlogCommentCreate, BlogCommentResponse
from common.schemas.pagination import PageResult
from common.config.database import get_db
from app.blog_service.service import blog_service

router = APIRouter(
    prefix="/blog",
    tags=["博客管理"],
    responses={404: {"description": "Not found"}}
)


@router.get("/page", summary="分页查询博客", response_model=PageResult[BlogResponse])
def query_blog_by_page(current_page: int = Query(1, ge=1, alias="currentPage", description="当前页码"),
                       page_size: int = Query(10, ge=1, le=100, alias="pageSize", description="每页数量"),
                       db: Session = Depends(get_db)):
    return blog_service.query_blog_by_page(db, current_page, page_size)


@router.get("/condition", summary="条件查询博客", response_model=List[BlogResponse])
def query_blog_by_condition(query: BlogQuery = Depends(),
                            db: Session = Depends(get_db)):
    return blog_service.query_blog_by_condition(db, query)


@router.get("/content/{blog_id}", summary="查询博客内容", response_model=BlogResponse)
def query_blog_by_id(blog_id: int, db: Session = Depends(get_db)):
    return blog_service.query_blog_by_id(db, blog_id)


@router.get("/category", summary="查询博客分类", response_model=List[BlogCategoryResponse])
def query_blog_category(db: Session = Depends(get_db)):
    return blog_service.query_blog_category(db)


@router.get("/stats", summary="查询博客统计信息", response_model=BlogStatsResponse)
def query_blog_stats(db: Session = Depends(get_db)):
    return blog_service.query_blog_stats(db)


@router.get("/latest", summary="查询近期博客", response_model=list[BlogLatestResponse])
def query_blog_latest(db: Session = Depends(get_db)):
    return blog_service.query_blog_latest(db)


@router.put("/comment", summary="新增博客评论", response_model=bool)
def add_blog_comment(blog_comment: BlogCommentCreate, request: Request, db: Session = Depends(get_db)):
    return blog_service.add_blog_comment(db, request, blog_comment)


@router.get("/comment/{blog_id}", summary="查询博客评论", response_model=list[BlogCommentResponse])
def query_blog_comment(blog_id: int, db: Session = Depends(get_db)):
    return blog_service.query_blog_comment(db, blog_id)